package com.dayuanit.dy14.enter.dy14enter1024.service.impl;

import com.dayuanit.dy14.enter.dy14enter1024.entity.User;
import com.dayuanit.dy14.enter.dy14enter1024.mapper.UserMapper;
import com.dayuanit.dy14.enter.dy14enter1024.service.EmailService;
import com.dayuanit.dy14.enter.dy14enter1024.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service
public class EmailServiceImpl extends BaseService implements EmailService {

    @Resource
    private UserMapper userMapper;

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    @Value("${from.email}")
    private String fromEmail;

    @Value("${email.subject}")
    private String subject;

    //创建一个固定个数的线程池
    private ExecutorService executorService = Executors.newFixedThreadPool(2);

    @Override
    public void batchSendEmail() {
        //1 查询所有的用户
        List<User> users = userMapper.listUser4Email();
        for (User user : users) {
            //2.循环发送邮件

            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "utf-8");

            try {
                logger.info("fromEmail=" + fromEmail);
                helper.setFrom(fromEmail);
                helper.setTo(user.getEmail());
                helper.setSubject(subject);
                helper.setText("<html><body><h1>恭喜你，亲爱的用户"+user.getUsername()+",你被分配到小组号: "+user.getGroupId()+"</h1></body></html>", true);
                javaMailSender.send(mimeMessage);
            } catch (MessagingException e) {
                e.printStackTrace();
            }

        }

    }


    @Override
    public void batchSendEmail2() {
        //1 查询所有的用户
        List<User> users = userMapper.listUser4Email();

        for (User user : users) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
                    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "utf-8");

                    try {
                        logger.info("fromEmail=" + fromEmail);
                        helper.setFrom(fromEmail);
                        helper.setTo(user.getEmail());
                        helper.setSubject(subject);
                        helper.setText("<html><body><h1>恭喜你，亲爱的用户"+user.getUsername()+",你被分配到小组号: "+user.getGroupId()+"</h1></body></html>", true);
                        javaMailSender.send(mimeMessage);
                    } catch (MessagingException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    /**
     * 线程池的解决方案
     */
    @Override
    public void batchSendEmail3() {
        //1 查询所有的用户
        List<User> users = userMapper.listUser4Email();

        for (User user : users) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
                    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "utf-8");

                    try {
                        logger.info("fromEmail=" + fromEmail);
                        helper.setFrom(fromEmail);
                        helper.setTo(user.getEmail());
                        helper.setSubject(subject);
                        helper.setText("<html><body><h1>恭喜你，亲爱的用户"+user.getUsername()+",你被分配到小组号: "+user.getGroupId()+"</h1></body></html>", true);
                        javaMailSender.send(mimeMessage);
                    } catch (MessagingException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}
